﻿using app.Common;
using eService.BLL;
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using System.Xml;

namespace eService.Presentation
{
    public partial class usrcCauHinh_Gia_PhuThuKhac : UserControl
    {
        private XmlDocument xmlDoc = new XmlDocument();
        public bool showPublic = false;
        ExcuteCAUHINH exe = new ExcuteCAUHINH();
        public usrcCauHinh_Gia_PhuThuKhac()
        {
            InitializeComponent();
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");
            LoadThongSo();
        }
        private void LoadThongSo()
        {
            try
            {
                bool PhuThuKM=false;
                string PhuThu="";
                int hinhthuctinh = exe.GetConfigPriceWhenChange();
                exe.getOtherMoney(out PhuThuKM, out PhuThu);
                string TamDung = AppSetting.getSetting().getConfigValue("Pause");
                string KhachVip = AppSetting.getSetting().getConfigValue("CusVIP");
                string LamTron = AppSetting.getSetting().getConfigValue("RoundValue");
                string BaoCaoTheo = AppSetting.getSetting().getConfigValue("TimeTypeReport");
               

                cmbXemBaoCaoTheo.SelectedIndex = int.Parse(BaoCaoTheo);
                chbTinhPhuThuKM.Checked = PhuThuKM;
                numPhuThu.Value = int.Parse(PhuThu);
                chbChoPhepTamDung.Checked = bool.Parse(TamDung);
                numLamTronSo.Value = int.Parse(LamTron);
                chbQuanLyKhachVIP.Checked = bool.Parse(KhachVip);

                if (hinhthuctinh == 1) radTinhGiaDoituongChuyenDen.Checked = true;
                else radTinhGiaTungSan.Checked = true;
            }
            catch
            { }
        }
        private void luuthongSo()
        {
            try
            {
                string HinhThucTinh = "", PhuThu = "", LamTron = "", PhuThuKM = "", KhachVip = "", BaoCaoTheo = "", TamDung = "";
                if (radTinhGiaDoituongChuyenDen.Checked) HinhThucTinh = "1";
                else HinhThucTinh = "0";
                PhuThu = numPhuThu.Value.ToString();
                LamTron = numLamTronSo.Value.ToString();
                PhuThuKM = chbTinhPhuThuKM.Checked.ToString();
                KhachVip = chbQuanLyKhachVIP.Checked.ToString();
                BaoCaoTheo = cmbXemBaoCaoTheo.SelectedIndex.ToString();
                TamDung = chbChoPhepTamDung.Checked.ToString();

                AppSetting.getSetting().setConfigValue("Pause", TamDung);
                AppSetting.getSetting().setConfigValue("CusVIP", KhachVip);
                AppSetting.getSetting().setConfigValue("RoundValue", LamTron);
                AppSetting.getSetting().setConfigValue("TimeTypeReport", BaoCaoTheo);

                UpdateKey("Pause", TamDung);
                UpdateKey("CusVIP", KhachVip);
                UpdateKey("RoundValue", LamTron);
                UpdateKey("TimeTypeReport", BaoCaoTheo);

                exe.UpdateConfigPriceWhenChange(int.Parse(HinhThucTinh));
                exe.updateOtherMoney(PhuThu, bool.Parse(PhuThuKM));

                ConfigurationManager.RefreshSection("appSettings");
                MessageBox.Show("Thông tin đã được cập nhật vào hệ thống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            catch
            { 
            
            }
        }
        public void UpdateKey(string strKey, string newValue)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            if (!this.KeyExists(strKey))
            {
                this.CreateKey(strKey);
            }
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    xmlNode2.Attributes["value"].Value = newValue;
                }
            }
            this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            this.xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        }
        public bool KeyExists(string strKey)
        {
            this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
            bool result;
            foreach (XmlNode xmlNode2 in xmlNode)
            {
                if (xmlNode2.Attributes["key"].Value == strKey)
                {
                    result = true;
                    return result;
                }
            }
            result = false;
            return result;
        }
        private void CreateKey(string strKey)
        {
            try
            {
                this.xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
                XmlNode xmlNode = this.xmlDoc.SelectSingleNode("configuration/appSettings");
                XmlNode xmlNode2;
                if (xmlNode.FirstChild != null)
                {
                    xmlNode2 = xmlNode.FirstChild.Clone();
                }
                else
                {
                    xmlNode2 = this.xmlDoc.CreateElement("add");
                    XmlAttribute node = this.xmlDoc.CreateAttribute("key");
                    XmlAttribute node2 = this.xmlDoc.CreateAttribute("value");
                    xmlNode2.Attributes.Append(node);
                    xmlNode2.Attributes.Append(node2);
                    xmlNode.AppendChild(xmlNode2);
                }
                xmlNode2.Attributes["key"].Value = strKey;
                xmlNode2.Attributes["value"].Value = "";
                xmlNode.AppendChild(xmlNode2);
                this.xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "eService.Presentation.exe.config");
            }
            catch
            {
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            luuthongSo();
        }
    }
}
